﻿/*
	事件功能: 無

	傳入值 : null
	傳出值 : xmlDoc
*/

    //宣告元件
    var xmlDoc;
    function addNode()
    {
       
        
        
	    if (xmlDoc == null) 
	    {
		    xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
		    xmlDoc.async = false;
		    xmlDoc.resolveExternals = false;
        }
    	
	    //create new element
	    var elem = xmlDoc.createElement(arguments[0]);	
    	
	    //start from the root element
	    if (xmlDoc.documentElement == null) 
	    {
		    xmlDoc.appendChild(elem);
	    }
	    else 
	    {
	        switch(arguments[0])
	        {
	            case 'command':
	                xmlDoc.documentElement.appendChild(elem);
	                break;
	            case 'parameter':
	                xmlDoc.documentElement.childNodes.item(0).appendChild(elem);
	                break;
	            default:
	                break;
	        }
	    }	
    	
	    //set attribute
        for (var i = 1; i < arguments.length - 1 ;i += 2) 
        {
            elem.setAttribute(arguments[i],ToUpperString(arguments[i + 1]));
        }
        
        
	    if ((arguments.length % 2) == 0) 
	    {
		    elem.text = ToUpperString(arguments[arguments.length - 1]);
	    }
    }

    /* 只有新增element，且沒有新增任何attributes時使用 */
    function setAttribute()
    {
	   
	    var elem = null;

        switch (arguments[0]) 
        {
            case 'form':
                elem = xmlDoc.selectNodes("//");
                setEmptyNode(elem,arguments);
                break;
            case 'command':
                elem = xmlDoc.selectNodes("//command");
                setEmptyNode(elem,arguments);
                break;
            case 'parameter':
                elem = xmlDoc.selectNodes("//parameter");
                setEmptyNode(elem,arguments);
                break;
            default:
                break;
        }
    }

    function getXML()
    {  
        var xmlReturn = xmlDoc.xml;
        xmlDoc = null;
        return xmlReturn;
    }

    function setEmptyNode(elem,arguments) 
    {
        for (var i = 0; i < elem.length; i++)
        {   
            
            var att = elem.item(i).attributes.length;
            if (att == 0) 
            {   
                for (var j = 1; j < arguments.length - 1; j += 2)
                {
                    elem.item(i).setAttribute(arguments[j],ToUpperString(arguments[j + 1]));
                }
                break;
            }
        }
        
        // set element Text
        if ((arguments.length % 2) == 0) 
	    {
		    elem.item(i).text = ToUpperString(arguments[arguments.length - 1]);
	    }
    }

    function addFormNode()
    {   
        
	    if (xmlDoc == null) 
	    {
		    xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
		    xmlDoc.async = false;
		    xmlDoc.resolveExternals = false;
        }
    	
	    //create new element
	    var elem = xmlDoc.createElement('form');	
    	
	    //start from the root element
	    if (xmlDoc.documentElement == null) 
	    {
		    xmlDoc.appendChild(elem);
	    }
    	
	    //set attribute
        elem.setAttribute("id",arguments[0]);
        elem.setAttribute("method",arguments[1]);
        if (arguments.length > 2) 
        {
            for (var i = 2; i < arguments.length - 1 ;i += 2) 
            {
                elem.setAttribute(arguments[i],ToUpperString(arguments[i + 1]));
            }
        }    
        
        //setNodeText
	    if ((arguments.length % 2) == 1) 
	    {
		    elem.text = ToUpperString(arguments[arguments.length - 1]);
	    }
    }

    function addCommandNode()
    {   
      
        
	    if (xmlDoc == null) 
	    {
		    xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
		    xmlDoc.async = false;
		    xmlDoc.resolveExternals = false;
        }
    	
	    //create new element
	    var elem = xmlDoc.createElement('command');	
    	
	    //set element nod
        xmlDoc.documentElement.appendChild(elem);
    	
	    //set attribute
        elem.setAttribute("id",arguments[0]);
        elem.setAttribute("action",arguments[1]);
        if (arguments.length > 2) 
        {
            for (var i = 2; i < arguments.length - 1 ;i += 2) 
            {
                elem.setAttribute(arguments[i],ToUpperString(arguments[i + 1]));
            }
        }    
        
        //setNodeText
	    if ((arguments.length % 2) == 1) 
	    {
		    elem.text = ToUpperString(arguments[arguments.length - 1]);
	    }	    
    }

    function addParameterNode()
    {   
        
	    if (xmlDoc == null) 
	    {
		    xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
		    xmlDoc.async = false;
		    xmlDoc.resolveExternals = false;
        }
    	
	    //create new element
	    var elem = xmlDoc.createElement('parameter');	
    	
	    //set element nod
        xmlDoc.documentElement.childNodes.item(0).appendChild(elem);
    	
	    //set attribute
        elem.setAttribute("id",arguments[0]);
        elem.setAttribute("type",arguments[1]);
        if (arguments.length > 2) 
        {
            for (var i = 2; i < arguments.length - 1 ;i += 2) 
            {
                elem.setAttribute(arguments[i],ToUpperString(arguments[i + 1]));
            }
        }    
        
        //setNodeText
	    if ((arguments.length % 2) == 1) 
	    {
		    elem.text = ToUpperString(arguments[arguments.length - 1]);
	    }
    }
    
      function addParameterChildNode()
    {  
        
        
	    if (xmlDoc == null) 
	    {
		    xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
		    xmlDoc.async = false;
		    xmlDoc.resolveExternals = false;
        }
    	
	    //create new element
	    var elem = xmlDoc.createElement('parameter');	
    	
	    //set element nod
        xmlDoc.documentElement.childNodes.item(1).appendChild(elem);
    	
	    //set attribute
        elem.setAttribute("id",arguments[0]);
        elem.setAttribute("type",arguments[1]);
        if (arguments.length > 2) 
        {
            for (var i = 2; i < arguments.length - 1 ;i += 2) 
            {
                elem.setAttribute(arguments[i],ToUpperString(arguments[i + 1]));
            }
        }    
        
        //setNodeText
	    if ((arguments.length % 2) == 1) 
	    {
		    elem.text = ToUpperString(arguments[arguments.length - 1]);
	    }
    }
    
/*
	<NOTE : 提供建立XML資料函式 >
*/

function XMLElement(InnerControler)
{
	this.XMLDocument = new ActiveXObject("Microsoft.XMLDOM");
	this.XMLDocument.async = false;
	
	this.Element = InnerControler;
	
	//create element 
	if(this.Element.value == ""){
		this.root= this.XMLDocument.createElement("DataTable");
		this.XMLDocument.appendChild(this.root);
	}
	else{
		this.XMLDocument.loadXML(this.Element.value)
		this.root = this.XMLDocument.documentElement;
	}
	//create XMLElement info
	
	this.addRow = Element_addRow;
	this.removeRow = Element_removeRow;
	this.queryRow = Element_queryRow;
	this.clear = Element_clear;
	this.dispose = Element_dispose;
}
/*	creater DataTable row */
function Row(ID)
{
	this.id = ID;
	this.IsRow = true;
	this.childerns = 0;
	this.children = new	 Array();
	this.addItem = Row_addItem;
	this.getItem = Row_getItem;
}

/*	add row Item */
function Row_addItem(Item){
	this.children[this.childerns] = Item;
	this.childerns++;
	return Item;
}
/* get Item */
function Row_getItem(Item,value)
{
	var newItem = new Object ();
	for(var i=0 ; i< this.children.length ; i++){
		if(this.children[i].getAttribute(Item) == value)
		{
			for(var j=0 ; j<this.children[i].attributes.length;j++){
				eval("newItem." + this.children[i].attributes(j).name + "=\""  + this.children[i].attributes(j).value +"\"");
			}
			return newItem;
		}		
	}
	return newItem;
}

/* create column object */
function Item(){
	this.arguments = arguments;
}
	
/* 	add XmlElement */
function Element_addRow(Row)
{
	//create row
	var RowNode = this.XMLDocument.createElement("row")
	RowNode.setAttribute("id", Row.id);
	
	//add colum attribute
	for(var i=0;i<Row.children.length;i++){
		var columNode = this.XMLDocument.createElement("item")
		for(var j=0 ; j < Row.children[i].arguments.length -1 ;j+=2)
		{
			columNode.setAttribute(Row.children[i].arguments[j],Row.children[i].arguments[j + 1]);
		}
		RowNode.appendChild(columNode);
	}
	this.root.appendChild(RowNode);
	this.Element.value =this.XMLDocument.xml;
	
}
/*
	remove Row
*/
function Element_removeRow(RowID)
{
	var xpath = "//DataTable/row[@id='" + RowID + "']";
	var row = this.XMLDocument.selectNodes(xpath);
	row.removeAll();
	this.Element.value =this.XMLDocument.xml;
}
/*
	clear element
*/
function Element_clear()
{
	this.Element.value = "";
}
/*
	query row
*/
function Element_queryRow(RowID)
{
	var xpath = "//DataTable/row[@id='" + RowID + "']";
	var XmlNode = this.XMLDocument.selectSingleNode(xpath);
	if(XmlNode!=null){
		var NewRow = new Row(RowID);
		for(var i=0 ;i<XmlNode.childNodes.length;i++){
			NewRow.children[i] = XmlNode.childNodes(i);
		}
		return NewRow;
	}
	return null;
}

/*
	dispose object
*/
function Element_dispose()
{
	this.XMLDocument  = null;
	this.Element = null;
	this.root = null;
}


